package cn.nr19.jian.`object`

import cn.nr19.jian.OnJianParserListener
import cn.nr19.jian.token.NULL
import cn.nr19.jian.exception.ParserException
import cn.nr19.jian.token.J2Node
import cn.nr19.jian.token.NFunNode

/**
 * 子类 ，没有事件功能定义。 创建时就必须引入父级类
 */
class JianLei2 : JianLeiApi {

    var parent : JianLeiApi

    // 变量、功能、事件
    val vars = HashMap<String,Any>()
    override fun curLeiVars(): HashMap<String, Any> = vars

    constructor(p : JianLeiApi){
        this.parent = p
    }
    constructor(p : JianLeiApi,x :Float,y : Float){
        this.parent = p
        p.setConf("x",x)
        p.setConf("y",y)
    }

    /**
     * 定义操作符转递变量
     */
    constructor(p : JianLeiApi, stat : J2Node, vararg vs : Any){
        this.parent = p
        // 若定义了变量名 则有几个读几个
        if (stat.vars.size > 0){
            for (i in 0 until stat.vars.size){
                if (vs.size > i) {
                    createVar(stat.vars[i],vs[i])
                }else{
                    createVar(stat.vars[i], NULL())
                }
            }
        }

        // 没定义 就默认 it 全部输入
        else{
            for (i in 0 until vs.size){
                createVar("it" + if (i == 0) "" else i,vs[i])
            }
        }
    }
    constructor(p : JianLeiApi, vs : EON){
        this.parent = p

        // 若定义了变量名 则有几个读几个
        vs.forEach {
            createVar(it.key,it.value)
        }

    }

    override fun getBaseUrl(url: String): String {
        return parent.getBaseUrl(url)
    }

    override fun getFun(name: String, pars: List<Any>): NFunNode? {
        return parent.getFun(name, pars)
    }

    override fun getEvent(name: String, parsSize: Int): J2Node? {
        return parent.getEvent(name, parsSize)
    }

    override fun vars(): Map<String, Any> {
        return HashMap<String,Any>().apply {
            putAll(parent.vars())
            putAll(vars)
        }
    }

    override fun events(): Map<String, J2Node> {
        return parent.events()
    }

    override fun funs(): List<NFunNode> {
        return parent.funs()
    }

    override fun getParserListener(): OnJianParserListener? {
        return parent.getParserListener()
    }

    override fun setConf(key: String, value: Any) = parent.setConf(key,value)
    override fun getConf(key: String): Any? = parent.getConf(key)
    override fun getFileAbsPath(path: String): String = parent.getFileAbsPath(path)



    override fun getVar(key: String): Any? {
        return vars[key] ?: parent.getVar(key)
    }

    override fun createVar(name: String, value: Any) {
        vars[name] = value
    }
    override fun setVar(name: String, value: Any){
        if (vars.containsKey(name)) vars[name] = value
        else parent.setVar(name, value)
    }
    override fun addFun(f: NFunNode) {
        throw ParserException("子类操作不可创建功能",0)
    }

    override fun addListener(name: String, j2: J2Node) {
        parent.addListener(name,j2)
    }
    override fun addImport(name: String, path: String) {
        parent.addImport(name, path)
    }

}